<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        //预解析案例
        //案例1:
        /*         var num = 10;
                fun();

                function fun() {
                    console.log(num); //undefined
                    var num = 20;
                }


                //根据预解析:函数提升 与变量提升 
                //相当于执行了以下代码
                var num;

                function fun() {
                    var num;
                    console.log(num);
                    num = 20;
                }
                num = 10;
                fun();


                //案例2
                var num = 10;

                function fn() {
                    console.log(num);
                    var num = 20;
                    console.log(num);
                }
                fn();
                //相当于执行了以下代码
                var num;

                function fn() {
                    var num;
                    console.log(num); //undefined
                    num = 20;
                    console.log(num); //20
                }
                num = 10;
                fn(); */

        //案例3
        /*     var a = 18;
            f1();

            function f1() {
                var b = 9;
                console.log(a);
                console.log(b);
                var a = '123';
            }
            //相当于执行了以下代码
            var a;

            function f1() {
                var b;
                var a;
                b = 9;
                console.log(a); //undefined
                console.log(b); //9
                a = '123';
            }
            a = 18;
            f1(); */

        //案例4
        f1();
        console.log(c);
        console.log(b);
        console.log(a);

        function f1() {
            var a = b = c = 9;
            //这里相当于 var a=9; b=9; c=9;
            console.log(a);
            console.log(b);
            console.log(c);
        }
        //相当于执行了以下代码
        function f1() {
            var a;
            a = b = c = 9; //这里b和c相当与特殊的全局变量
            console.log(a); //9
            console.log(b); //9
            console.log(c); //9
        }
        f1();
        console.log(c); //9
        console.log(b); //9
        console.log(a); // a is not defined(局部变量,外部不能调用到)
    </script>
</head>

<body>

</body>

</html>